package com.duke.shaking.register;

import java.util.Timer;
import java.util.TimerTask;

import com.duke.shaking.R;
import com.duke.shaking.TitleActivity;
import com.duke.shaking.net.AsyncHttpResponseWithResultHandler;
import com.duke.shaking.net.NetUtils;
import com.duke.shaking.pojo.User;
import com.duke.shaking.utils.AppUtils;
import com.duke.shaking.utils.DeviceUtils;
import com.duke.shaking.utils.PrefUtils;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;
import android.widget.Toast;

public class Register1Activity extends TitleActivity {
	private User regUser;
	private Activity activity;
	
	
	
	public Register1Activity() {
		this.activity = this;
	}

	@Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        
        Intent i = getIntent();
        regUser = (User)i.getSerializableExtra("regUser");
        if (regUser == null || regUser.getSex() == User.sex_unk) {
        	toast(R.string.err_no_sex);
        }
        
        String telNo = DeviceUtils.geTelNumber();
        EditText txtTelNo = (EditText)findViewById(R.id.edit_tel_no);
        if(telNo != null) {
        	txtTelNo.setText(telNo);
        }
        
        txtTelNo.setFocusable(true);
        txtTelNo.setFocusableInTouchMode(true);
        txtTelNo.requestFocus();
        
        AppUtils.popupSoftKeyboard(this);
        
    }
	
	public void onRightBtn(View v) {
		if (regUser == null || regUser.getSex() == User.sex_unk) {
			return;
		}
		String telNo = null;
		EditText txtTelNo = (EditText)findViewById(R.id.edit_tel_no);
		if (txtTelNo != null) {
			telNo = txtTelNo.getText().toString();
		}
		if (!AppUtils.isValidMobile(telNo)) {
			toast(R.string.err_invalid_telno);
			return;
		}
		telNo = telNo.trim();
		regUser.setTelNo(telNo);
		
		AsyncHttpResponseWithResultHandler responseHandler = new AsyncHttpResponseWithResultHandler(){
			
        	@Override
			public void onResultSuccess(String response) {
				// TODO Auto-generated method stub
				super.onResultSuccess(response);
				
				/*PrefUtils.getInstance(activity, activity.getString(R.string.registerPref))
					.addString(activity.getString(R.string.verifyCode), aQuery.id(R.id.edit_secret).getText().toString())
					.commit();*/
				
				go(Register2Activity.class, "regUser", regUser);
			}

			@Override
			public void onResultFailure(String result) {
				// TODO Auto-generated method stub
				super.onResultFailure(result);
				
				Toast.makeText(activity, R.string.err_send_telno, Toast.LENGTH_LONG).show();
			}
        };
        
        NetUtils.getVerifyCode(this, responseHandler, regUser.getTelNo());
		
	}

	@Override
	protected void onTitleInit() {
		// TODO Auto-generated method stub
		initTitle(TITLE_BTN_EXIST | TITLE_BTN_BACK,
				R.string.back,
				TITLE_BTN_EXIST,
				R.string.next,
				R.string.title_fill_telno);
	}

	@Override
	protected int getLayoutId() {
		// TODO Auto-generated method stub
		return R.layout.activity_register1;
	}
}
